package com.poisonh.unifiedauth.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.PreferencesUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;
import com.poisonh.unifiedauth.widget.CircleView;

import org.json.JSONObject;

/**
 * Created by PoisonH on 2016/7/4.
 */
public class CircleFragment extends BaseFragment
{
    private CircleView mCircleView;

//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
//    {
//        @SuppressLint("InflateParams")
//        View view = inflater.inflate(R.layout.fragment_circle, null);
//        return view;
//    }

    @Override
    protected View loadView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.fragment_circle, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        mCircleView = (CircleView) view.findViewById(R.id.circleview);
        getAppLoginOther();
    }

    /**
     * 获取数据
     */
    private void getAppLoginOther()
    {
        HttpParams params = new HttpParams();
     //   String mStrUserId = PreferencesUtils.getString(getActivity(), "ID");
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.GETAPPLOGINOTHER, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                Message message = Message.obtain();
                message.obj = response;
                mHandler.sendMessage(message);
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(getContext(), "数据请求失败");
            }
        });
    }

    Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            try
            {
                String mStrResponse = (String) msg.obj;
                JSONObject object = new JSONObject(mStrResponse);
                int untreatedCount = object.getInt("untreatedCount");
                int failureCount = object.getInt("failureCount");
                int time_save = object.getInt("time_save");
                int minutes = time_save / 60;
                int seconds = time_save % 60;
                String string = getString(R.string.save_time_txt, minutes, seconds);
                String string2 = getString(R.string.times_txt, untreatedCount, failureCount);
                mCircleView.setSubtitleText(string);
                mCircleView.setSubtitleText_(string2);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    };
}
